﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CallSignAnalysis
{
    public partial class FmSettings : Form
    {
        public FmSettings()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
            RWini.SetPath("config.ini");

            string up_lotw = RWini.ReadValue("upload", "lotw_quiet");
            string up_eqsl = RWini.ReadValue("upload", "eqsl_quiet");
            string up_qrz = RWini.ReadValue("upload", "qrz_quiet");

            // 判断 up_lotw 是否为 "true"，如果是则设置 cbox_eqsl_tip.Checked 为 true
            cbox_lotw_tip.Checked = up_lotw.ToLower() == "true" ? true : false;

            // 判断 up_eqsl 是否为 "true"，如果是则设置 cbox_eqsl_tip.Checked 为 true
            cbox_eqsl_tip.Checked = up_eqsl.ToLower() == "true" ? true : false;

            cbox_qrz_tip.Checked = up_qrz.ToLower() == "true" ? true : false;

            cbox_lotw_enable.Checked = RWini.ReadValue("switch", "ban_lotw").ToLower() == "true" ? true : false;
            cbox_eqsl_enable.Checked = RWini.ReadValue("switch", "ban_eqsl").ToLower() == "true" ? true : false;
            cbox_qrz_enable.Checked = RWini.ReadValue("switch", "ban_qrz").ToLower() == "true" ? true : false;


            RWini.SetPath(g.lang + ".ini");
            getControlFromName(this);
        }

        private Control getControlFromName(Control container)
        {
            foreach (Control c in container.Controls)
            {
                if (c is Sunny.UI.UIButton || c is Sunny.UI.UICheckBox || c is Sunny.UI.UILabel || c is Button || c is Sunny.UI.UIGroupBox)
                {
                    string t = RWini.ReadValue(c.Name, "Text");
                    if (!string.IsNullOrWhiteSpace(t))
                    {
                        c.Text = t;
                        string name = RWini.ReadValue(c.Name, "Font");
                        int size = RWini.ReadInt(c.Name, "Size", 16);
                        string Sizes = RWini.ReadValue(c.Name, "Size");
                        int.TryParse(Sizes, out size);
                        if (!string.IsNullOrWhiteSpace(name))
                        {
                            FontFamily ff = new FontFamily(name);
                            FontStyle style = FontStyle.Bold;
                            // style |= FontStyle.Regular;
                            // style |= FontStyle.Italic;, GraphicsUnit.World
                            c.Font = new Font(ff, size, style);
                        }

                    }
                }
                if (c.HasChildren)
                {
                    //这里不能直接写return  getControlFromName(c, controlName);因为会导致只执行第一个子容器控件,如果未找到就提前返回了Null;
                    Control myControl = getControlFromName(c);
                    if (myControl != null)
                    {
                        return myControl;
                    }
                }
            }
            //并非所有的代码路径都返回值,未找到就返回null
            return null;
        }

        private void cbox_lotw_tip_CheckedChanged(object sender, EventArgs e)
        {
        }
        private void cbox_eqsl_tip_CheckedChanged(object sender, EventArgs e)
        {
        }

        private void smr_close_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void smr_confirm_Click(object sender, EventArgs e)
        {
            RWini.SetPath("config.ini");
            RWini.WriteValue("upload", "lotw_quiet", cbox_lotw_tip.Checked.ToString().ToLower());
            RWini.WriteValue("upload", "eqsl_quiet", cbox_eqsl_tip.Checked.ToString().ToLower());
            RWini.WriteValue("upload", "qrz_quiet", cbox_qrz_tip.Checked.ToString().ToLower());
            RWini.WriteValue("switch", "ban_lotw", cbox_lotw_enable.Checked.ToString().ToLower());
            RWini.WriteValue("switch", "ban_eqsl", cbox_eqsl_enable.Checked.ToString().ToLower());
            RWini.WriteValue("switch", "ban_qrz", cbox_qrz_enable.Checked.ToString().ToLower());

            this.DialogResult = DialogResult.OK;
        }

        private void cbox_qrz_tip_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
